export function loadMore (element:HTMLDivElement,callback){
  // scollHeight = clientHeight + scollTop
  let timer
  element.addEventListener('scroll',()=>{
    // 防抖
    if(timer){
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      let clientHeight = element.clientHeight
      let scollHeight = element.scrollHeight
      let scollTop = element.scrollTop
      if(clientHeight + scollTop+10>=scollHeight)  callback()    
    }, 500)
  })
}

export function downFresh(element:any,callback:any){
  let startY:number;//按下时候的初始纵坐标
  let distance:number;//一共下拉的距离
  let originalTop = element.offsetTop;//最初的元素距离 父级顶部的距离
  // 节流
  let startTime
  element.addEventListener('touchstart',function(event:any){
      if(element.offsetTop == originalTop &&element.scrollTop == 0){
       startY = event.touches[0].pageY;
       startTime = Date.now() // 记录开始时间
       element.addEventListener('touchmove',touchMove);
       element.addEventListener('touchend',touchEnd);
      }
  });
  function touchMove(event:any){
    if(Date.now()-startTime>50){
      startTime = Date.now()
      let pageY  = event.touches[0].pageY;
      if(pageY>startY){
        distance = pageY - startY;
        element.style.top = (originalTop+distance)+'px';
      }else{
        element.removeEventListener('touchmove',touchMove);
        element.removeEventListener('touchend',touchEnd);
      }
    }  
  }
  function touchEnd(event:any){
   element.removeEventListener('touchmove',touchMove);
   element.removeEventListener('touchend',touchEnd);
   let timer = setInterval(function(){
       if(distance < 1){
           element.style.top =  originalTop+'px';
           clearInterval(timer);
       }else{
           element.style.top =  (originalTop+--distance)+'px';
       }
   },0);
   if(distance>10){
       callback();
   }
  }

}